<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\ceping\controller;

use app\admin\model\CepingCategoryModel;
use app\admin\model\CepingModel;
use cmf\controller\RestBaseController;

class IndexController extends RestBaseController
{
    /**
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $page = $this->request->param('page');
        $keyword = $this->request->param('keyword');
        $category_id = $this->request->param('category_id');
        $ceping = new CepingModel();
        $cate = new CepingCategoryModel();
        $categories = $cate->all();
        $query = $ceping;
        if (trim($keyword)) {
            $query = $ceping->where('product_name', 'like', '%'.$keyword.'%');
        }
        if ($category_id){
            $query = $ceping->where('category_id', $category_id);
        }
        $list = $query->paginate(6, false, ['page' => $page]);
        foreach ($list as $v) {
            $v['product_image'] = cmf_get_image_preview_url($v['product_image']);
            $v['shop_image'] = cmf_get_image_preview_url($v['shop_image']);
            $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
            foreach ($categories as $c_v) {
                if ($v['category_id'] == $c_v['id']) {
                    $v['category'] = $c_v['name'];
                }
            }
        }

        $this->success('获取成功!', $list);
    }

    public function get_categories(){
        $cate = new CepingCategoryModel();
        $categories = $cate->all();
        $this->success('获取成功!', $categories);
    }
}
